﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Bussines.Utils;
using Entities;

namespace Website.Administrator
{
    public partial class DanhMucList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataDropdowList(ddlDmCha, 2);
                if (Request.QueryString["MaDanhMucCha"] != null)
                {

                    if (Request.QueryString["danhmuccap2"] != null)
                    {
                        DanhMucService danhMucService = new DanhMucService();
                        DanhMuc danhMuc = new DanhMuc();
                        danhMuc = danhMucService.Select(Int32.Parse(Request.QueryString["MaDanhMucCha"]));
                        ddlDmCha.SelectedValue = danhMuc.Pid.ToString();
                        LoadDataGrv();
                        for (int i = 0; i < GridView1.Rows.Count; i++)
                        {
                            if (GridView1.Rows[i].Cells[1].Text == danhMuc.Id.ToString())
                            {
                                GridView1.SelectedIndex = i;
                                i = GridView1.Rows.Count;
                            }
                        }

                        LoadDataGrvCon(Int32.Parse(Request.QueryString["danhmuccap2"]));
                    }
                    else
                    {
                        ddlDmCha.SelectedValue = Request.QueryString["MaDanhMucCha"];
                        LoadDataGrv();
                    }

                }
                else
                {

                    LoadDataGrv();
                }


                ImageButton1.Attributes.Add("onClick", "javascript:return confirm('Bạn có chắc chắn muốn Xóa theo lựa chọn?');");
            }
            else
            {

                if (cbxTimKiem.Checked)
                {
                    LoadDataGrv();
                    GridView2.DataSource = null;
                    GridView2.DataBind();
                }
                else
                {
                    int maDmCha;
                    try
                    {
                        if (GridView1.SelectedIndex >= 0)
                        {
                            if (Int32.TryParse(GridView1.SelectedRow.Cells[1].Text, out maDmCha))
                                if (maDmCha > 0)
                                    LoadDataGrvCon(maDmCha);
                        }
                    }
                    catch (Exception)
                    { }
                }
            }
        }

        protected void ddlDmCha_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTen.Text = "";
            LoadDataGrv();
            GridView2.DataSource = null;
            GridView2.DataBind();
        }
        private void LoadDataDropdowList(DropDownList downList, int maDanhMuc)
        {

            DanhMucService danhMucService = new DanhMucService();
            List<DanhMuc> danhMucs = new List<DanhMuc>();
            Hashtable hashtable=new Hashtable();
            hashtable["pid"] = 2;
            danhMucs = danhMucService.Search(hashtable,"id","DESC",0);            
            downList.DataSource = danhMucs;
            downList.DataTextField = "Ten";
            downList.DataValueField = "ID";
            downList.DataBind();

        }
        private void LoadDataGrv()
        {
            DanhMucService danhMucService = new DanhMucService();
            int pid = Int32.Parse(ddlDmCha.SelectedValue.ToString());
            List<DanhMuc> danhMucs = new List<DanhMuc>();
            Hashtable hashtable = new Hashtable();
            if (pid > 0)
            { hashtable["pid"] = pid; }
            hashtable["ten"] = "%" + txtTen.Text + "%";
            danhMucs = danhMucService.Search(hashtable, "id", "DESC", 0);
            GridView1.DataSource = danhMucs;
            GridView1.DataBind();
            
            LinkButton lb;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {               
                lb = (LinkButton)GridView1.Rows[i].Cells[4].Controls[0];
                MessageBox.confrm(lb);
            }
        }
        private void LoadDataGrvCon(int pid)
        {
            DanhMucService danhMucService = new DanhMucService();
            List<DanhMuc> danhMucs = new List<DanhMuc>();
            Hashtable hashtable = new Hashtable();
            if (pid > 0)
            { hashtable["pid"] = pid; }
            hashtable["ten"] = "%" + txtTen.Text + "%";
            danhMucs = danhMucService.Search(hashtable, "id", "DESC", 0);
            GridView2.DataSource = danhMucs;
            GridView2.DataBind();
         
            
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {               
            //    lb = (LinkButton)GridView1.Rows[i].Cells[4].Controls[0];
             //   MessageBox.confrm(lb);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in GridView1.Rows)
            {
                ToggleCheckState(true);
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in GridView1.Rows)
            {
                ToggleCheckState(false);
            }
        }
        private void ToggleCheckState(bool checkState)
        {
            // Iterate through the Products.Rows property
            foreach (GridViewRow row in GridView1.Rows)
            {
                // Access the CheckBox
                CheckBox cb = (CheckBox)row.FindControl("ProductSelector");
                if (cb != null)
                    cb.Checked = checkState;
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadDataGrv();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            String str = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
            Response.Redirect("DanhMucDetail.aspx?MaDanhMucEdit=" + str);
        }

        protected void imgXoaLuaChon(object sender, ImageClickEventArgs e)
        {
            DanhMucService danhMucService = new DanhMucService();
            DanhMuc danhMuc = new DanhMuc();
            int ma;
            bool check = false;
            GridViewRowCollection gridViewRow = GridView1.Rows;
            foreach (GridViewRow gr in gridViewRow)
            {
                CheckBox cb = (CheckBox)gr.Cells[1].FindControl("ProductSelector");
                cb = (CheckBox)gr.FindControl("ProductSelector");
                if (cb.Checked)
                {
                    if (Int32.TryParse(gr.Cells[1].Text, out ma))
                    {
                        danhMuc.Id = ma;
                        danhMucService.Delete(danhMuc);
                        check = true;
                    }
                }
            }
            if (check)
            {
                MessageBox.Show("Xoa thanh cong!");
                LoadDataGrv();
            }
        }



        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int pid = Int32.Parse(GridView1.Rows[e.NewSelectedIndex].Cells[1].Text);
            LoadDataGrvCon(pid);
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            int pid = Int32.Parse(GridView1.SelectedRow.Cells[1].Text);
            LoadDataGrvCon(pid);
        }

        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DanhMucService danhMucService = new DanhMucService();
            DanhMuc danhMuc = danhMucService.Select(Int32.Parse(GridView2.Rows[e.RowIndex].Cells[1].Text));
            danhMucService.Delete(danhMuc);
            MessageBox.Show("Xoa Thanh Cong!");
            GridView2.DataSource = null;
            GridView2.DataBind();

        }

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {

            String maCap2 = GridView2.Rows[e.NewEditIndex].Cells[1].Text;
            String maCap1 = GridView1.SelectedRow.Cells[1].Text;
            Response.Redirect("DanhMucDetail.aspx?Macap1=" + maCap1 + "&macap2=" + maCap2);
        }



        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            DanhMucService danhMucService = new DanhMucService();
            DanhMuc danhMuc = new DanhMuc();
            danhMuc.Id = Int32.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
            danhMucService.Delete(danhMuc);
            MessageBox.Show("Xoa Thanh Cong!");
            LoadDataGrv();

        }






    }
}